<?php

use Illuminate\Database\Seeder;
use App\Models\Product;
use App\Models\Type;

class ProductsTableSeeder extends Seeder
{
    public function run()
    {
        // 获取 Faker 实例
        $faker = app(Faker\Generator::class);

        $products = factory(Product::class)->times(500)->make();

        Product::insert($products->toArray());


        $products = Product::all();
        // 所有用户 ID 数组，如：[1,2,3,4]
        $type_ids = Type::all()->pluck('id')->toArray();

        $products->each(function ($product, $index) use ($type_ids, $faker) {
            $product->types()->attach($faker->randomElement($type_ids));
        });
    }

}

